﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml.Linq;
using Microsoft.SharePoint;
using System.Drawing;

namespace SPThumbnailExtender.Interne
{
    internal class ThumbnailPowerPoint:ThumbnailBase
    {
        public ThumbnailPowerPoint(Uri uri)
            : base(uri)
        {
            this.Width = 640;
            this.Height = 480;
        }
        public ThumbnailPowerPoint(Uri uri, int Width, int Height)
            : base(uri, Width, Height)
        { }
        public override System.Drawing.Bitmap GenerateThumbnail()
        {
            return GenerateThumbnail(0);
        }

        public override System.Drawing.Bitmap GenerateThumbnail(int Page)
        {
            string strWebUrl;
            string strDoc;
            using (SPSite siteCollection = new SPSite(this.FileUri.AbsoluteUri))
            {
                using (SPWeb myWeb = siteCollection.OpenWeb())
                {
                    strWebUrl = myWeb.Url;
                    strDoc = this.FileUri.AbsoluteUri.Replace(siteCollection.Url, "");
                }
            }

            Uri uri = new Uri(strWebUrl + "/_layouts/MobilePresentation.ashx?presentationId=" + strDoc, UriKind.Absolute);

            Bitmap bmpToWrite = null;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
            {
                string resultString = streamReader1.ReadToEnd();
                XDocument xdoc = XDocument.Parse(resultString);

                string strImageUrl = xdoc.Element(XName.Get("mobilePres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("slideImage", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Attribute("url").Value;
                string strImageID = xdoc.Element(XName.Get("mobilePres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("pres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("sldLst", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Elements(XName.Get("sld", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).ElementAt(Page).Attribute("sldImg").Value;

                string dimension = string.Format("&width={0}&height={1}", this.Width, this.Height);

                string strUrl = strWebUrl + "/_layouts/" + strImageUrl + "&imageId=" + strImageID + dimension;
                Uri uriBitmap = new Uri(strUrl);
                Helper.GetBitmapFromUri(uriBitmap, out bmpToWrite);
                Helper.DeleteThumbnail(this.FileUri, Page);
                Helper.InsertThumbnail(this.FileUri, Page, bmpToWrite);
            }
            return bmpToWrite;
        }

        public override int GetCount()
        {
            string strWebUrl;
            string strDoc;
            using (SPSite siteCollection = new SPSite(this.FileUri.AbsoluteUri))
            {
                using (SPWeb myWeb = siteCollection.OpenWeb())
                {
                    strWebUrl = myWeb.Url;
                    strDoc = this.FileUri.AbsoluteUri.Replace(siteCollection.Url, "");
                }
            }

            Uri uri = new Uri(strWebUrl + "/_layouts/MobilePresentation.ashx?presentationId=" + strDoc, UriKind.Absolute);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            int iPageCount;
            using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
            {
                string resultString = streamReader1.ReadToEnd();
                XDocument xdoc = XDocument.Parse(resultString);
                iPageCount=xdoc.Element(XName.Get("mobilePres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("pres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("sldLst", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Elements(XName.Get("sld", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Count();
                
            }
            return iPageCount;

        }

        public override void GenerateThumbnails()
        {
            int iPageCount=GetCount();
            Helper.DeleteThumbnailsForDocument(this.FileUri);
            for (int miind = 0; miind < iPageCount; miind++)
            {
                object o = Helper.GetParameter("SPThumbnailExtender.NbPage");
                int iNBPage;
                if (o == null)
                    iNBPage = 0;
                else
                    int.TryParse(o.ToString(), out iNBPage);

                if (iNBPage > 0 && miind > iNBPage)
                {
                    break;
                }

                GenerateThumbnail(miind);
            }
        }
    }
}
